<?php
/*
 * 2021/7/16
 * 7:06
 * User: 2650332702@qq.com
 */

namespace App\Api\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table='cmc_goods';
    //获得一条商品
    static public function OnceGood($id,$allow_field)
    {
        return Goods::where('goods_id',$id)->get($allow_field)->toArray()[0];
    }

    public function getGoodsImageAttribute($value)
    {
        return config('aliyun.cui').$this->attributes['goods_image'];
    }
    /**
     * 查询方法
     * @param $hid 请求查询id
     * @return mixed
     */
    protected static function QueryContent($hid,$allowField)
    {
        return self::where('goods_class',$hid)->select($allowField)->get()->toArray();
    }

    /**
     * 查询爆款类名和对应值
     * @param $allow_field
     * @return mixed
     */
    public static function getTabInformation($allowField)
    {
        return self::select($allowField)->distinct()->get()->toArray();
    }

    /**
     * 获取到对应爆款类的数据
     * @param $hid //前台发送选项卡数据
     * @return mixed
     */
    public static function getGoodsTab($hid,$allowField)
    {
        //判断所属类别获取数据
        return self::QueryContent($hid,$allowField);
    }

    /**
     * 分类下数据
     * @param $categoryId 分类id
     * @param $allowField 所需字段
     */
    public static function getPageData($categoryId,$allowField)
    {
        return self::select($allowField)->where('cate_id',$categoryId)->where('goods_state',1)->get()->toArray();
    }

    /**
     * 查询分类排行榜数据
     * @param $category_id 分类id
     * @param $allow_field 查询字段
     * @return mixed
     */
    public static function getRankContent($categoryId,$allowField)
    {
        return self::select($allowField)->where('cate_id',$categoryId)->limit(10)->orderBy('sale_num','desc')->get()->toArray();
    }

    /**
     * 获得对应分类的数据并排行
     * @param $category_id 分类id
     */
    public static function getRankingData($hid,$allowField)
    {
        return self::QueryContent($hid,$allowField);
    }

    /**
     * 推荐商品
     * @return string
     */
    static function getPopularList($allowField)
    {
        $popuData = self::select($allowField)->orderBy('sale_num','desc')->get()->toArray();

        return $popuData;

    }

    //获取商品信息
    static public function getCartGoods($gid,$allowField)
    {
        return self::where('goods_id',$gid)->get($allowField)->toArray()[0];
    }


    /**
     * 销量排行榜
     * @param $allow_field
     */
    public static function getRankList()
    {
        $SecKillData = CmcUserSeckill::getStoreSeckill();
        foreach ($SecKillData as $v){
            $goods[] = Seckill::secInGoods($v['seckill_id'],'goods_id');
        }

        $BargainData = PromotionBargainLaunch::getBragainGoods();
        foreach ($BargainData as $v){
            $goods[] = PromotionBargainLaunch::bargainList($v['bargain_id'],'goods_id');
        }


        $PintuanData = CmcPromotionPintuanOrder::getPintuanGoods();

        foreach ($PintuanData as $v){
            $goods[] = CmcPromotionPintuanOrder::pintuanList($v['pintuan_id'],'goods_id');
        }

        return $goods;
    }

    public static function getYestSaleList()
    {
        $SecKillData = CmcUserSeckill::getStoreSeckill();
        foreach ($SecKillData as $v){
            $goods[] = CmcUserSeckill::getYearStoreSeckill($v['seckill_id']);
        }

        $BargainData = PromotionBargainLaunch::getBragainGoods();
        foreach ($BargainData as $v){
            $goods[] = PromotionBargainLaunch::getYearBragainGoods($v['bargain_id']);
        }


        $PintuanData = CmcPromotionPintuanOrder::getPintuanGoods();

        foreach ($PintuanData as $v){
            $goods[] = CmcPromotionPintuanOrder::getYearPintuanGoods($v['pintuan_id']);
        }

        return $goods;
    }

    /**
     * cuiqun
     * 查询商品对应的优惠券
     * @param $id
     * @return mixed
     */
    static public function getListGoodsCoupon($id){
        return self::where('cmc_goods.goods_id',$id)->value('coupon_id');
    }
}
